package gson;

import java.lang.reflect.Field;

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import bean.DaShuai;

public class gsonSample {

	public static void main(String[] args) {
		DaShuai lidashuai = new DaShuai();
		lidashuai.setAge(22.3);
		lidashuai.setBirthday("2016-09.09");
		lidashuai.setComment("这是一个注释");
		lidashuai.setName("李大帅");
		lidashuai.setHas_girlfriend(false);
		lidashuai.setCar(null);
		lidashuai.setHouse(null);
		lidashuai.setMajor(new String[] {"挖掘机","编程"});
		lidashuai.setSchool("蓝翔");
		lidashuai.setIgnore("不要看见我！");
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();
		gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
			
			@Override
			public String translateName(Field f) {
				if(f.getName().equals("name")){
					return "NAME";
				}
				return f.getName();
			}
		});
		Gson gson = gsonBuilder.create();
		System.out.println(gson.toJson(lidashuai));
	}
}
